package sanger;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

import java.io.DataInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Download extends HttpServlet {

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        Utilities.logOrRedir(request.getSession(), request, response);
        
        
        ServletOutputStream out = null;
        DataInputStream in = null;
        String fileName = request.getParameter("n");
        String prefix = request.getSession().getAttribute("uname") + "_";
        AmazonS3 s3 = Constants.getS3Obj();

        if (fileName == null) {
            System.out.println("parameter null");
            return;
        }

        if (fileName.equals("")) {
            System.out.println("parameter is empty");
            return;
        }


        try {

            byte[] buff = new byte[4096]; //4kb
            int len = 0;
            out = response.getOutputStream();
            S3Object object = s3.getObject(new GetObjectRequest(Constants.BUCKET, Constants.AWS_DIR + prefix + fileName));
            in = new DataInputStream(object.getObjectContent());

            response.setContentType(object.getObjectMetadata().getContentType());
            response.setContentLength((int) object.getObjectMetadata().getContentLength());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

            while ((in != null) && ((len = in.read(buff)) != -1)) {
                out.write(buff, 0, len);
            }



        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                    + "to Amazon S3, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with S3, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        } finally {
            out.close();
            in.close();

        }
    }
}
